﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MahApps.Metro.Controls;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor
{
    /// <summary>
    /// EntryInfoInputBox.xaml 的交互逻辑
    /// </summary>
    public partial class EntryInfoInputBox : MetroWindow
    {
        /// <summary>
        /// 显示一个文件信息输入框。
        /// </summary>
        /// <param name="promptTitle">输入标题的提示文本。</param>
        /// <param name="promptFileName">输入文件名的提示文本。</param>
        /// <param name="defTitle">默认标题文本。</param>
        /// <param name="defFileName">默认文件短名。</param>
        /// <param name="defAutoConvertToPinYin">是否自动将标题文本转换成拼音首字母作为文件名。</param>
        /// <param name="owner">主窗口。一般传入 Globals.MainWindow 即可。</param>
        /// <returns></returns>
        public static LEntryInfo Show(string winTitle = "", string promptTitle = "", string promptFileName = "", string defTitle = "", string defFileName = "", string prompt = "", bool defAutoConvertToPinYin = true, Window owner = null)
        {
            var fii = new EntryInfoInputBox()
            {
                Owner = (owner == null ? Globals.MainWindow : owner),
            };

            if (string.IsNullOrWhiteSpace(winTitle))
            {
                fii.Title = Globals.AppName;
            }
            else
            {
                fii.Title = winTitle;
            }

            if (string.IsNullOrWhiteSpace(promptTitle) == false)
            {
                fii.tbTitle.Text = promptTitle;
            }

            if (string.IsNullOrWhiteSpace(defTitle) == false)
            {
                fii.tbxTitle.Text = defTitle;
            }

            if (string.IsNullOrWhiteSpace(promptFileName) == false)
            {
                fii.tbFileName.Text = promptFileName;
            }

            if (string.IsNullOrWhiteSpace(defFileName) == false)
            {
                fii.tbxFileName.Text = defFileName;
            }

            if (string.IsNullOrWhiteSpace(prompt) == false)
            {
                fii.rdPrompt.Height = new GridLength(1, GridUnitType.Auto);
                fii.tbPrompt.Text = prompt;
            }
            else
            {
                fii.rdPrompt.Height = new GridLength(0, GridUnitType.Pixel);
                fii.tbPrompt.Text = "";
            }

            fii.ckxAutoConvertToPinYin.IsChecked = defAutoConvertToPinYin;

            if (fii.ShowDialog() == true)
            {
                if (string.IsNullOrEmpty(fii.tbxFileName.Text))   // 文件短名不能为空或空白字符。
                {
                    return null;
                }
                else
                {
                    return new LEntryInfo()
                    {
                        Title = (string.IsNullOrWhiteSpace(fii.tbxTitle.Text) ? fii.tbxFileName.Text : fii.tbxTitle.Text),   // 如果没有输入标题文本，则以文件短名为标题文本。
                        EntryName = fii.tbxFileName.Text,     // 文件短名不能为空或空白字符。
                    };
                }
            }
            else return null;
        }

        public EntryInfoInputBox()
        {
            InitializeComponent();

            this.Title = Globals.AppName + " - 设置文件信息";
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
            this.Close();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        private void tbxTitle_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter || e.Key == Key.Tab)
            {
                tbxFileName.SelectAll();
                tbxFileName.Focus();
            }
        }

        private void tbxFileName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                this.DialogResult = true;
                this.Close();
            }
        }

        private void tbxTitle_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (ckxAutoConvertToPinYin.IsChecked == true)
            {
                tbxFileName.Text = ChinesePinYin.ToChinesePinYinText(tbxTitle.Text);
            }
        }

        private void ckxAutoConvertToPinYin_Checked(object sender, RoutedEventArgs e)
        {
            if (ckxAutoConvertToPinYin.IsChecked == true)
            {
                tbxFileName.Text = ChinesePinYin.ToChinesePinYinText(tbxTitle.Text);
            }
        }

        private void MetroWindow_Loaded(object sender, RoutedEventArgs e)
        {
            tbxTitle?.Focus();
            tbxTitle?.SelectAll();
        }
    }

    public class LEntryInfo
    {
        public string Title { get; set; } = "";
        public string EntryName { get; set; } = "";
    }
}
